//
//  MeInfoTableViewCell.m
//  Cat
//
//  Created by lushitong on 2018/11/29.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "MeInfoTableViewCell.h"

@interface MeInfoTableViewCell()

@property (nonatomic, strong) UIImageView  *leftImageView;
@property (nonatomic, strong) UILabel *leftInfoLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, strong) UIImageView *redPointView;

@end

@implementation MeInfoTableViewCell

- (void)setCellModelData:(id)cellModelData {
	NSDictionary *dic = (NSDictionary *)cellModelData;
	if ([dic[@"imageName"] length] == 0) {
		self.arrowImageView.hidden = YES;
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		self.cellSeparatorStyle = QKUITableViewCellSeparatorStyleNone;
		self.contentView.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
		return;
	}
	self.leftImageView.image = [UIImage imageNamed:dic[@"imageName"]];
	self.leftInfoLabel.text = dic[@"leftText"];
	self.rightLabel.text = dic[@"rightText"] ? dic[@"rightText"] : @"";
}

- (void)setIsRedPoint:(BOOL)isRedPoint {
	_isRedPoint = isRedPoint;
	self.redPointView.hidden = !isRedPoint;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		self.cellSeparatorStyle = QKUITableViewCellSeparatorStyleSingleLineBottom;
		self.selectionStyle = UITableViewCellAccessoryDisclosureIndicator;
		[self loadSubviews];
	}
	return self;
}

- (void)loadSubviews {
	[self.contentView addSubviews:@[self.leftImageView,
									self.leftInfoLabel,
									self.rightLabel,
									self.redPointView,
									self.arrowImageView]];
	
	[self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(24), kQKFitModuleDecimal(24)));
		make.centerY.equalTo(self.contentView.mas_centerY);
		make.left.equalTo(self.contentView.left).offset(kQKFitModuleDecimal(24));
	}];
	
	[self.leftInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(self.leftImageView.centerY);
		make.left.equalTo(self.leftImageView.mas_right).offset(kQKFitModuleDecimal(20));
	}];
	
	[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(self.contentView.mas_right).offset(-kQKFitModuleDecimal(42));
		make.centerY.equalTo(self.contentView.mas_centerY);
	}];
	
	[self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(16), kQKFitModuleDecimal(16)));
		make.centerY.equalTo(self.leftImageView.mas_centerY);
		make.right.equalTo(self.contentView.mas_right).offset(-kQKFitModuleDecimal(21));
	}];
	
	[self.redPointView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.mas_equalTo(CGSizeMake(kQKFitModuleDecimal(10), kQKFitModuleDecimal(10)));
		make.right.equalTo(self.arrowImageView.mas_left).offset(-kQKFitModuleDecimal(2.0));
		make.centerY.equalTo(self.contentView.mas_centerY);
	}];
	
	[self.bottomLineView mas_updateConstraints:^(MASConstraintMaker *make) {
		make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0.5 * SWScaleFromInch47);
	}];
}

#pragma mark----- lazy init -----

- (UIImageView *)leftImageView {
	if (!_leftImageView) {
		_leftImageView = [[UIImageView alloc] init];
	}
	return _leftImageView;
}

- (UILabel *)leftInfoLabel {
	if (!_leftInfoLabel) {
		_leftInfoLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x4A4A4A)];
	}
	return _leftInfoLabel;
}

- (UILabel *)rightLabel {
	if (!_rightLabel) {
		_rightLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
		_rightLabel.textAlignment = NSTextAlignmentRight;
	}
	return _rightLabel;
}

- (UIImageView *)arrowImageView {
	if (!_arrowImageView) {
		_arrowImageView = [[UIImageView alloc] init];
		_arrowImageView.image = [UIImage imageNamed:@"arrow_right"];
	}
	return _arrowImageView;
}

- (UIImageView *)redPointView {
	if (!_redPointView) {
		_redPointView = [[UIImageView alloc] init];
		_redPointView.image = [UIImage imageWithColor:COLOR_WITH_HEX(0xE1483D)];
		_redPointView.layer.cornerRadius = kQKFitModule(5);
		_redPointView.layer.masksToBounds = YES;
		_redPointView.hidden = YES;
	}
	return _redPointView;
}

@end
